package com.example.design.action.memento.client;

import com.example.design.action.memento.Admin;
import com.example.design.action.memento.ConfigFile;
import com.example.design.action.memento.ConfigMemento;
import com.example.design.action.memento.ConfigOriginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;


import java.util.Date;

/**
 * 客户端调用
 */

public class MementoClient {
   public static Logger logger = LoggerFactory.getLogger(MementoClient.class);


    public static void main(String[] args) {


        Admin admin = new Admin();
        ConfigOriginator configOriginator = new ConfigOriginator();

        configOriginator.setConfigFile(new ConfigFile("1000001", "配置内容A=哈哈", new Date(), "小哥"));
        admin.append(configOriginator.saveMemento());

        configOriginator.setConfigFile(new ConfigFile("1000002", "配置内容A=嘻嘻", new Date(), "小哥"));
        // 保存配置
        admin.append(configOriginator.saveMemento());

        configOriginator.setConfigFile(new ConfigFile("1000003", "配置内容A=么么", new Date(), "小哥"));
        // 保存配置
        admin.append(configOriginator.saveMemento());

        configOriginator.setConfigFile(new ConfigFile("1000004", "配置内容A=嘿嘿", new Date(), "小哥"));
        // 保存配置
        admin.append(configOriginator.saveMemento());

        // 历史配置(回滚)
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置(回滚)undo：{}", JSON.toJSONString(configOriginator.getConfigFile()));

        // 历史配置(回滚)
        configOriginator.getMemento(admin.undo());
        logger.info("历史配置(回滚)undo：{}", JSON.toJSONString(configOriginator.getConfigFile()));

        // 历史配置(前进)
        configOriginator.getMemento(admin.redo());
        logger.info("历史配置(前进)redo：{}", JSON.toJSONString(configOriginator.getConfigFile()));

        // 历史配置(获取)
        configOriginator.getMemento(admin.get("1000002"));
        logger.info("历史配置(获取)get：{}", JSON.toJSONString(configOriginator.getConfigFile()));



    }


}
